<?php

/**
 * 后台链接模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: linkAction.php 1402 2011-03-03 10:15:34Z zhangwentao $
 * http://www.t-mac.org；
 */
class linkAction extends Action
{

    private $tmp_model;
    private $category_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('admin.link');
        $this->category_model = Tmac::model('admin.category');
        $check_model = $this->M('admin.check');
        $check_model->checkLogin();
        $check_model->CheckPurview('i_admin');
    }

    /**
     * 资讯类别管理 首页
     */
    public function index()
    {
        //连接所属分类
        $cat_id = $this->getParam('cat_id');
        $search_keyword = $this->H($this->getParam('search_keyword'));
        $link_do = $this->getParam('link_do');
        $link_recommend = $this->getParam('link_recommend');

        $treers = $this->category_model->tree(0, 0, $cat_id);

        //取友情链接类型radiobutton数组
        $link_type_ary = UtilityConfig::link_type_ary();
        $link_type_ary_option = Utility::Option($link_type_ary, $link_recommend);

        //取友情操作类型radiobutton数组
        $link_do_ary = UtilityConfig::link_do_ary();
        $link_do_ary_option = Utility::Option($link_do_ary, $link_do);

        //TODO  取出所有资讯
        $rs = $this->tmp_model->getlinkList('', $cat_id, $link_recommend, $search_keyword);
        $this->assign('treers', $treers);
        $this->assign('link_type_ary_option', $link_type_ary_option);
        $this->assign('link_do_ary_option', $link_do_ary_option);
        $this->AV('link');
    }

    /**
     * 新增/修改链接
     */
    public function add()
    {
        $link_id = !empty($_GET['link_id']) ? (int) $_GET['link_id'] : 0;
        if ($link_id > 0) {
            $editinfo = $this->tmp_model->getlinkInfo($link_id);
        } else {
            $editinfo = array();
            $editinfo['link_type_radio'] = '';
            $editinfo['link_state_radio'] = '';
            $editinfo['link_target'] = '_blank';
            $editinfo['link_recommend'] = '0';
        }
        //取链接类型radiobutton数组
        $link_type_radio_ary = UtilityConfig::link_type_radio_ary();
        $link_type_radio_option = Utility::RadioButton($link_type_radio_ary, 'link_type_radio', $editinfo['link_type_radio'], 'onclick="typechange(this.value);"');

        //取广告状态option数组
        $link_state_radio_ary = UtilityConfig::ad_state_radio_ary();
        $link_state_radio_option = Utility::RadioButton($link_state_radio_ary, 'link_state_radio', $editinfo['link_state_radio'], 'onclick="statechange(this.value);"');

        //取链接打开类型数组
        $link_target_radio_ary = UtilityConfig::link_target_radio_ary();
        $link_target_radio_option = Utility::RadioButton($link_target_radio_ary, 'link_target_radio', $editinfo['link_target'], '');

        //取链接评论状态
        $link_recommend_radio_ary = UtilityConfig::link_recommend_radio_ary();
        $link_recommend_radio_option = Utility::CheckboxButton($link_recommend_radio_ary, 'link_recommend_radio', $editinfo['link_recommend'], '');

        //初始化一下    默认state_radio
        $editinfo['link_order'] = !empty($editinfo['link_order']) ? $editinfo['link_order'] : '0';
        $editinfo['link_url'] = !empty($editinfo['link_url']) ? $editinfo['link_url'] : 'http://';
        $editinfo['link_starttime'] = !empty($editinfo['link_starttime']) ? date('Y-m-d', $editinfo['link_starttime']) : '';
        $editinfo['link_endtime'] = !empty($editinfo['link_endtime']) ? date('Y-m-d', $editinfo['link_endtime']) : '';
        $editinfo['link_name'] = !empty($editinfo['link_name']) ? $editinfo['link_name'] : '';
        $editinfo['link_id'] = !empty($editinfo['link_id']) ? $editinfo['link_id'] : '';
        $link_image = !empty($editinfo['link_image']) ? $editinfo['link_image'] : '';
        $editinfo['externallinks'] = $editinfo['link_type_radio'] == 3 ? $link_image : '';
        $editinfo['link_image'] = $editinfo['link_type_radio'] == 2 ? $link_image : '';
        $editinfo['link_description'] = !empty($editinfo['link_description']) ? $editinfo['link_description'] : '';
        $editinfo['cat_id'] = !empty($editinfo['cat_id']) ? $editinfo['cat_id'] : '0';

        //连接所属分类
        $treers = $this->category_model->tree(0, 0, $editinfo['cat_id']);

        $this->assign('link_type_radio_option', $link_type_radio_option);
        $this->assign('link_state_radio_option', $link_state_radio_option);
        $this->assign('link_target_radio_option', $link_target_radio_option);
        $this->assign('link_recommend_radio_option', $link_recommend_radio_option);
        $this->assign('treers', $treers);
        $this->assign('editinfo', $editinfo);

        $this->AV('link');
    }

    /**
     * 新增/修改链接页面　保存　
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 5) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $link_id = !empty($_POST['link_id']) ? (int) $_POST['link_id'] : 0;
        $link_name = !empty($_POST['title']) ? trim($this->H($_POST['title'])) : '';
        $link_url = !empty($_POST['link']) ? trim($this->H($_POST['link'])) : '';
        $link_type_radio = !empty($_POST['link_type_radio']) ? $_POST['link_type_radio'] : '';
        $link_upload = !empty($_POST['thumb']) ? $_POST['thumb'] : '';
        $link_externallinks = !empty($_POST['externallinks']) ? trim($this->H($_POST['externallinks'])) : '';
        $link_order = !empty($_POST['order']) && is_numeric($_POST['order']) ? $_POST['order'] : '0';
        $link_state_radio = !empty($_POST['link_state_radio']) ? $_POST['link_state_radio'] : '';
        $link_starttime = !empty($_POST['starttime']) ? strtotime($_POST['starttime']) : '';
        $link_endtime = !empty($_POST['endtime']) ? strtotime($_POST['endtime']) : '';
        $link_target = !empty($_POST['link_target_radio']) ? $_POST['link_target_radio'] : '';
        $link_description = !empty($_POST['link_description']) ? $_POST['link_description'] : '';
        $link_recommend = !empty($_POST['link_recommend_radio']) ? $_POST['link_recommend_radio'] : '0';
        $cat_id = !empty($_POST['cat_id']) ? $_POST['cat_id'] : '0';

        //判断友链类型
        if ($link_type_radio == 2) {
            $link_image = $link_upload;
        } elseif ($link_type_radio == 3) {
            $link_image = $link_externallinks;
        } else {
            $link_image = '';
        }

        /* 验证不能为空 */
        if (empty($link_name)) {
            $this->redirect('链接标题不能为空');
            exit;
        }
        if (empty($link_url)) {
            $this->redirect('链接不能为空');
            exit;
        }
        if (!isset($link_type_radio)) {
            $this->redirect('选择链接类型!');
            exit;
        }
        if (!isset($link_state_radio)) {
            $this->redirect('选择链接状态!');
            exit;
        }
        // TODO goon to verify

        $info = array(
            'link_name' => $link_name,
            'link_url' => $link_url,
            'link_type_radio' => $link_type_radio,
            'link_image' => $link_image,
            'link_order' => $link_order,
            'link_state_radio' => $link_state_radio,
            'link_starttime' => $link_starttime,
            'link_endtime' => $link_endtime,
            'link_target' => $link_target,
            'link_description' => $link_description,
            'link_recommend' => $link_recommend,
            'cat_id' => $cat_id,
            'link_time' => time()
        );

        if ($link_id > 0) {
            $link_page = HttpResponse::getCookie('link_page');
            //update save article
            $where = 'link_id = ' . $link_id . '';
            $rs = $this->tmp_model->addlink($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改链接成功', 'index.php?m=admin/link&page= ' . $link_page . '');
            } else {
                $this->redirect('修改链接失败');
            }
        } else {
            //insert save article_class
            $rs = $this->tmp_model->addlink($info);
            if ($rs) {
                $this->redirect('添加链接成功', 'index.php?m=admin/link');
            } else {
                $this->redirect('添加链接失败');
            }
        }
    }

    public function link_do()
    {
        $link_do = $this->getParam('link_do');
        $id_a = $this->getParam('id_a');
        if ($link_do == 'del') {
            $this->del();
        } else {
            if (is_array($id_a)) {
                $id = implode(',', $id_a);
            } elseif (!empty($do_id)) {
                $id = $do_id;
            } else {
                $this->redirect('请选择要操作的链接...');
                exit;
            }
            if ($link_do == 'no_recomment') {
                $link_recommend = 0;
                $rs_info = '非推荐选定';
            } elseif ($link_do == 'yes_comment') {
                $link_recommend = 1;
                $rs_info = '推荐选定的';
            } else {
                $rs_info = '';
            }
            $info = array(
                'link_recommend' => $link_recommend
            );
            //update save article
            $where = 'link_id in (' . $id . ')';
            $rs = $this->tmp_model->addlink($info, 'UPDATE', $where);
            // TODO 处理完
            if ($rs) {
//                $comment_page = HttpResponse::getCookie('comment_page');
                $this->redirect($rs_info . '链接成功');
            } else {
                $this->redirect($rs_info . '链接失败，请重试！');
            }
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function del()
    {
        $do_id = $this->getParam('link_id');
        $id_a = !empty($_POST['id_a']) ? $_POST['id_a'] : '';
        if (is_array($id_a)) {
            $id = implode(',', $id_a);
        } elseif (!empty($do_id)) {
            $id = $do_id;
        } else {
            $this->redirect('请选择要删除的...');
            exit;
        }
        $rs = $this->tmp_model->dellink($id);
        // TODO DEL该分类下的所有资讯
        if ($rs) {
            $link_page = HttpResponse::getCookie('link_page');
            $this->redirect('删除链接成功', 'index.php?m=admin/link&page=' . $link_page . '');
        } else {
            $this->redirect('删除链接失败，请重试！');
        }
    }

    /**
     * Ajax修改推荐状态
     */
    public function changeRecommentType()
    {
        $link_id = !empty($_POST['link_id']) ? (int) $_POST['link_id'] : 0;
        $type = !empty($_POST['type']) ? (int) $_POST['type'] : 0;

        if ($type == 0) {
            $link_recommend = 1;
        } elseif ($type == 1) {
            $link_recommend = 0;
        } else {
            $link_recommend = 0;
        }
        $info = array(
            'link_recommend' => $link_recommend
        );
        //update save article
        $where = 'link_id = ' . $link_id . '';
        $rs = $this->tmp_model->addlink($info, 'UPDATE', $where);
        if ($rs) {
            if ($link_recommend == 1) {

                $data = "<a class='yes' title='点击取消推荐' onclick=\"changeRecommentType($link_id,$link_recommend)\" style='cursor:pointer'>推荐</a>";
            } elseif ($link_recommend == 0) {
                $data = "<a class='no' title='点击设为推荐' onclick=\"changeRecommentType($link_id,$link_recommend)\" style='cursor:pointer'>不是推荐</a>";
            }
            $error = 0;
        } else {
            $error = 1;
            $data = '';
        }
        $data = array('id' => $link_id, 'data' => $data, 'error' => $error);
        $rs = json_encode($data);
        exit($rs);
    }

    /**
     * Ajax修改排序
     */
    public function changeOrder()
    {
        $link_id = $this->getParam('link_id');
        $link_order = $this->getParam('link_order');
        $info = array(
            'link_order' => $link_order
        );
        //update save article
        $where = 'link_id = ' . $link_id . '';
        $rs = $this->tmp_model->addlink($info, 'UPDATE', $where);
        if ($rs) {
            $error = 0;
        } else {
            $error = 1;
        }
        $data = array('link_id' => $link_id, 'link_order' => $link_order, 'error' => $error);
        $rs = json_encode($data);
        exit($rs);
    }

}